package com.qf.lanzhou.blog.api.service.impl;

import com.qf.lanzhou.blog.api.dao.Impl.login.UserDaoImpl;
import com.qf.lanzhou.blog.api.dao.UserDao;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.UserService;
import com.qf.lanzhou.blog.api.util.MD5Utils;

import java.util.List;

/**
 * @author Yeach-chen
 * @ClassName UserServiceImp
 */
public class UserServiceImp implements UserService {
    private UserDao userDao=new UserDaoImpl();
    @Override
    public List<User> getlist() {
        List<User> list = userDao.showUserList();
        if (list.size()==0){
            throw new RuntimeException("系统维护中...请稍等");
        }
        return list;
    }

    @Override
    public int deleteUser(String userName) {
        int result = userDao.deleteUserByUserName(userName);
        if (result==0){
            throw new RuntimeException("删除失败,请联系管理员...");
        }
        return result;
    }

    @Override
    public int modifyUser(String password,User user) {
        String enPassword = MD5Utils.stringToMD5(password);
        if (user.getPassword().equals(enPassword)){
            throw new RuntimeException("新密码不可以与原密码相同,请重新修改！");
        }
        int result = userDao.updatePassword(enPassword, user.getUserName());
        if (result==0){
            throw new RuntimeException("密码修改失败，请联系管理员");
        }
        return result;
    }

    @Override
    public int modifyUserMsg(String email, String phone, User user) {
        if (email.equals(user.getEmail())){
            throw new RuntimeException("新邮箱与原邮箱相同,修改失败");
        }
        if (phone.equals(user.getPhone())){
            throw new RuntimeException("新手机号码与原手机号码相同,修改失败");
        }
        int result = userDao.udateMsg(email,phone,user);
        if (result==0){
            throw new RuntimeException("个人信息修改失败，请联系管理员");
        }
        return result;
    }

    @Override
    public User getByUserName(String userName) {
        User user = userDao.selectUserByUserName(userName);
        if (user==null){
            throw new RuntimeException("服务器异常，请稍后..");
        }
        return user;
    }

    @Override
    public void UserEixts(User user) {
        if (user==null){
            throw new RuntimeException("您未登陆！何以退出？");
        }
    }

    @Override
    public boolean checkAdmin(Long userId) {
        String sql="SELECT count(*) FROM r_user_role WHERE user_id=? AND role_id =100;";
        System.out.println("判断是否为管理员");
        Long num = userDao.checkAdmin(sql, userId);
        System.out.println("判断是否为管理员后------------");
        if (num==0L){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkAdminFull() {
        String sql="SELECT count(*) FROM r_user_role WHERE role_id=100 GROUP BY role_id";
        Long num = userDao.checkAminFull(sql);
        if (num>=6){
            return true;
        }
        return false;
    }

    @Override
    public int addAdmin(Long userId) {
        int num = userDao.updateUserRole(userId);
        if (num==0){
            throw new RuntimeException("系统繁忙，请稍后..");
        }
        return num;
    }
}
